from rest_framework import serializers

from apps.PersonInfo.models import PersonInfoModel
from apps.login.models import UserInfo


# 用户信息序列化器
class PersonInfoSerializers(serializers.ModelSerializer):

    def validate(self, attrs):
        return attrs

    class Meta:
        model = PersonInfoModel
        exclude = ['user_token']

        # 重写update方法用于修改用户信息

    # def update(self, instance, validated_data):
    #     instance.birthday = validated_data.get('department')
    #     instance.gender = validated_data.get('major')
    #     instance.mobile = validated_data.get('classRoom')
    #     instance.email = validated_data.get('sex')
    #     instance.username = validated_data.get('wechat')
    #     instance.username = validated_data.get('hobby')
    #     instance.save()
    #     return instance













class PersonInfoSerializers1(serializers.Serializer):
    department = serializers.CharField(label="所在院系")
    major = serializers.CharField(label="专业")
    classroom = serializers.CharField(label="班级")
    sex = serializers.CharField(label="性别",default="未知")
    wechat = serializers.CharField(label="微信号")
    hobby = serializers.CharField(label="爱好")
    user_token = serializers.Serializer(label="用户token",read_only=True)

    def validate(self, attrs):
        if not attrs["user_token"]:
            raise serializers.ValidationError("用户登录")
        return attrs


    def create(self, validated_data):
        user_token = self.context.get('request').user_token
        print(user_token)
        data = PersonInfoModel(**validated_data)
        data.save()
        return validated_data